package com.controller.crud;

import com.pojo.Department;
import com.pojo.Employee;
import com.service.DepartmentService;
import com.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;

@Controller
@RequestMapping("/user")
public class EmployeeController {


    @Autowired
    EmployeeService employeeService;
    @Autowired
    DepartmentService departmentService;
    @RequestMapping("/findAllEmps")
    public String getAllEmployee(Model model,HttpServletRequest request){
        Collection<Employee> employees = employeeService.queryAll();
        for (Employee employee : employees) {
            System.out.println(employee);
        }
        model.addAttribute("employees",employees);
        /*model.addAttribute("msg","jiayou!");*/
        String dele = (String)request.getSession().getAttribute("dele");
        System.out.println("dele已经: "+dele);

        return "emp/list";
    }
    @GetMapping("/emp")
    public String toAdd(Model model){
        Collection<Department> departments = departmentService.queryAll();
        model.addAttribute("departments",departments);
        return "emp/add";
    }
    @PostMapping("/emp")
    public String add(Employee employee,HttpServletRequest request){
        System.out.println(employee);
        employee.getDepartment().setDepartmentName(departmentService.
                queryById(employee.getDepartment().getId()).getDepartmentName());
        employeeService.addUser(employee);
        request.getSession().setAttribute("dele","添加成功");
        return "redirect:/user/findAllEmps";
    }
    @GetMapping("/emp/{id}")
    public String toupd(@PathVariable("id") Integer id,Model model){
        Employee employee = employeeService.queryById(id);

        model.addAttribute("employee",employee);
        Collection<Department> departments = departmentService.queryAll();
        model.addAttribute("departments",departments);
        return "emp/udp";
    }
    @PostMapping("udp")
    public String upd(Employee employee,HttpServletRequest request){
        System.out.println("当前员工："+employee.toString());
       /* Employee(id=10000, lastName=魏明稳, emall=changanwei266@gmail.com,
        gender=1, department=Department(id=4, departmentName=null), birth=Thu May 21 00:00:00 CST 2020)*/
        //request.getSession().setAttribute("dele","修改成功");
       employee.getDepartment().setDepartmentName(employeeService.queryById(employee.getId()).getDepartment().getDepartmentName());

        employeeService.updataUser(employee);
        return "redirect:/user/findAllEmps";
    }
   @RequestMapping("/del")
    public String del(@RequestParam("id") Integer id, Model model, HttpServletRequest request){
        employeeService.delUser(id);
        request.getSession().setAttribute("dele","删除成功");
       /* model.addAttribute("dele","删除成功");//相当于request，一次请求过后就没有了*/
       return "redirect:/user/findAllEmps";
   }
}
